package com.hkfn.rfb.rfb.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hkfn.rfb.R;
import com.hkfn.rfb.common.utils.Utils;
import com.hkfn.rfb.common.activity.BaseActivity;
import com.hkfn.rfb.common.adapter.BaseRecyclerAdapter;
import com.hkfn.rfb.rfb.bean.RfbDataModel;
import com.hkfn.rfb.rfb.bean.RfbDataMonitorModel;

import java.util.List;

public class RfbDataMonitorAdapter extends BaseRecyclerAdapter<RfbDataMonitorModel> {

    private BaseActivity activity;

    public RfbDataMonitorAdapter(BaseActivity activity) {
        this.activity = activity;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(activity).inflate(R.layout.item_rfb_data_monitor, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, RfbDataMonitorModel data) {
        if (viewHolder instanceof MyViewHolder) {
            setMyViewData((MyViewHolder) viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_item_content;
        private TextView tv_form_title;
        private View v_form_title_line;
        private TextView tv_cell_2;
        private TextView tv_cell_3;
        private TextView tv_cell_4;
        private TextView tv_cell_5;
        private TextView tv_cell_6;
        private TextView tv_cell_7;
        private TextView tv_cell_8;
        private TextView tv_cell_9;
        private TextView tv_cell_10;
        private TextView tv_cell_11;
        private TextView tv_cell_12;
        private TextView tv_cell_13;
        private TextView tv_cell_14;
        private TextView tv_cell_15;
        private TextView tv_cell_16;
        private TextView tv_cell_17;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            initMyView(this, itemView);
        }
    }

    private void initMyView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_form_title = itemView.findViewById(R.id.tv_form_title);
        myViewHolder.v_form_title_line = itemView.findViewById(R.id.v_form_title_line);
        myViewHolder.tv_cell_2 = itemView.findViewById(R.id.tv_cell_2);
        myViewHolder.tv_cell_3 = itemView.findViewById(R.id.tv_cell_3);
        myViewHolder.tv_cell_4 = itemView.findViewById(R.id.tv_cell_4);
        myViewHolder.tv_cell_5 = itemView.findViewById(R.id.tv_cell_5);
        myViewHolder.tv_cell_6 = itemView.findViewById(R.id.tv_cell_6);
        myViewHolder.tv_cell_7 = itemView.findViewById(R.id.tv_cell_7);
        myViewHolder.tv_cell_8 = itemView.findViewById(R.id.tv_cell_8);
        myViewHolder.tv_cell_9 = itemView.findViewById(R.id.tv_cell_9);
        myViewHolder.tv_cell_10 = itemView.findViewById(R.id.tv_cell_10);
        myViewHolder.tv_cell_11 = itemView.findViewById(R.id.tv_cell_11);
        myViewHolder.tv_cell_12 = itemView.findViewById(R.id.tv_cell_12);
        myViewHolder.tv_cell_13 = itemView.findViewById(R.id.tv_cell_13);
        myViewHolder.tv_cell_14 = itemView.findViewById(R.id.tv_cell_14);
        myViewHolder.tv_cell_15 = itemView.findViewById(R.id.tv_cell_15);
        myViewHolder.tv_cell_16 = itemView.findViewById(R.id.tv_cell_16);
        myViewHolder.tv_cell_17 = itemView.findViewById(R.id.tv_cell_17);
    }

    private void setMyViewData(MyViewHolder viewHolder, RfbDataMonitorModel data, int position) {
        // 根据最长的文字内容强制设置 Item 根布局宽度
        ViewGroup.LayoutParams params = viewHolder.itemView.getLayoutParams();
        if (position == 0){
            params.width = (int) calculateWidthBasedOnData(data.getDataModelList()) + Utils.dip2px(activity, 140);
        }else{
            params.width = (int) calculateWidthBasedOnData(data.getDataModelList()) + Utils.dip2px(activity, 100);
        }

        viewHolder.itemView.setLayoutParams(params);


        List<RfbDataModel> dataModelList = data.getDataModelList();
        viewHolder.tv_form_title.setText(dataModelList.get(0).getDataValue());
        if (position == getItemCount() - 1){
            viewHolder.v_form_title_line.setVisibility(View.GONE);
        }else {
            viewHolder.v_form_title_line.setVisibility(View.VISIBLE);
        }
        setCellValue(viewHolder.tv_cell_2, dataModelList, 1);
        setCellValue(viewHolder.tv_cell_3, dataModelList, 2);
        setCellValue(viewHolder.tv_cell_4, dataModelList, 3);
        setCellValue(viewHolder.tv_cell_5, dataModelList, 4);
        setCellValue(viewHolder.tv_cell_6, dataModelList, 5);
        setCellValue(viewHolder.tv_cell_7, dataModelList, 6);
        setCellValue(viewHolder.tv_cell_8, dataModelList, 7);
        setCellValue(viewHolder.tv_cell_9, dataModelList, 8);
        setCellValue(viewHolder.tv_cell_10, dataModelList, 9);
        setCellValue(viewHolder.tv_cell_11, dataModelList, 10);
        setCellValue(viewHolder.tv_cell_12, dataModelList, 11);
        setCellValue(viewHolder.tv_cell_13, dataModelList, 12);
        setCellValue(viewHolder.tv_cell_14, dataModelList, 13);
        setCellValue(viewHolder.tv_cell_15, dataModelList, 14);
        setCellValue(viewHolder.tv_cell_16, dataModelList, 15);
        setCellValue(viewHolder.tv_cell_17, dataModelList, 16);
    }

    private void setCellValue(TextView tv_cell, List<RfbDataModel> dataModelList, int index){
        if (dataModelList.size() > index){
            tv_cell.setVisibility(View.VISIBLE);
            tv_cell.setText(dataModelList.get(index).getDataValue());
        }else {
            tv_cell.setVisibility(View.GONE);
        }
    }

    /**
     * 获取最长文字宽度
     * @param dataModelList
     * @return
     */
    private float calculateWidthBasedOnData(List<RfbDataModel> dataModelList){
        String maxTextStr = "";
        for (RfbDataModel model:dataModelList) {
            if (maxTextStr.length() < model.getDataValue().length()){
                maxTextStr = model.getDataValue();
            }
        }
        float textViewContentLength = Utils.getTextViewContentLength(15, maxTextStr);
        return textViewContentLength;
    }

}
